import 'dart:convert';

import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_json_view/flutter_json_view.dart';

class BeautyJsonPage extends StatefulWidget {
  const BeautyJsonPage({super.key});

  @override
  State<BeautyJsonPage> createState() => _BeautyJsonPageState();
}

class _BeautyJsonPageState extends State<BeautyJsonPage> {
  TextEditingController controller = TextEditingController();
  TextEditingController controller2 = TextEditingController();
  dynamic data;
  int index = 0;
  List<String> replaceList = [
    '[        ] flutter: ',
    '[        ] I/flutter (27450): ',
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        backgroundColor: Theme.of(context).colorScheme.inversePrimary,
        title: const Text('Json 修复工具'),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          var text = controller.text.replaceAll(controller2.text, '');
          text = text.replaceAll(RegExp(r'[\r\n]'), '');
          try {
            controller.text = text;
            Clipboard.setData(ClipboardData(text: text));
            ScaffoldMessenger.of(context).showSnackBar(
              const SnackBar(
                content: Text('复制成功'),
              ),
            );
            data = text;
            setState(() {});
          } catch (e) {
            if (kDebugMode) {
              print(e);
            }
          }
        },
        child: const Center(
          child: Text('修复'),
        ),
      ),
      body: SafeArea(
        child: Column(
          children: [
            Row(
              children: [
                TextButton(
                  onPressed: () {
                    controller.text = '';
                    data = null;
                    setState(() {});
                  },
                  child: const Text('清空'),
                ),
                Flexible(
                  child: Container(
                    color: Colors.white,
                    child: TextField(
                      controller: controller2,
                      maxLines: 1,
                      decoration: const InputDecoration(
                        hintText: '请输入替换值',
                        contentPadding: EdgeInsets.all(16),
                        border: InputBorder.none,
                      ),
                    ),
                  ),
                )
              ],
            ),
            Row(
              children: [
                TextButton(
                    onPressed: () {
                      index = 0;
                      controller2.text = replaceList[index];
                      setState(() {});
                    },
                    child: Text(replaceList[0])),
                SizedBox(
                  height: 20,
                  child: Container(
                    color: Colors.red,
                    height: 20,
                    width: 2,
                  ),
                ),
                TextButton(
                    onPressed: () {
                      setState(() {
                        index = 1;
                        controller2.text = replaceList[index];
                      });
                    },
                    child: Text(replaceList[1])),
              ],
            ),
            if (data != null)
              Expanded(
                child: JsonView.string(data),
              ),
            Expanded(
              child: SingleChildScrollView(
                child: Container(
                  color: Colors.white,
                  child: TextField(
                    controller: controller,
                    maxLines: null,
                    decoration: const InputDecoration(
                      hintText: '请输入Json',
                      contentPadding: EdgeInsets.all(16),
                      border: InputBorder.none,
                    ),
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}
